package com.healthcloud.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.healthcloud.entity.PatientSNEntity;
import com.healthcloud.exception.ReturnException;
import com.healthcloud.service.PatientSNService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 *
 * 人和设备绑定表
 * Created by yucl on 2017/7/20.
 */
@ApiIgnore
@Api(value = "示例说明", description = "人和设备绑定表")
@RestController
@RequestMapping("/app/patientSN")
public class PatientSNController {

	@Autowired
	private PatientSNService patientSNService;

    /**
     * 设备绑定
     *
     * @param  patientSNEntity
     * @return
     * @throws Exception 
     */
	@ApiOperation(value = "设备绑定",notes ="设备绑定")
	@RequestMapping(value = "/binding", method = RequestMethod.POST)
    public ApiRes binding(PatientSNEntity patientSNEntity) {
        try {
			patientSNService.binding(patientSNEntity);
		} catch (ReturnException e) {
			return new ApiRes(false, null, null, e.getMessage());
		}
        return ApiRes.SUCCESS;
    }

    /**
     * 设备解除绑定
     *
     *
     * @param  patientSNEntity
     * @return
     */
	@ApiOperation(value = "设备解除绑定",notes ="设备解除绑定")
	@RequestMapping(value = "/relieve", method = RequestMethod.POST)
    public ApiRes relieve(PatientSNEntity patientSNEntity){
		try {
			patientSNService.relieve(patientSNEntity);
		} catch (ReturnException e) {
			return new ApiRes(false, null, null, e.getMessage());
		}
        return ApiRes.SUCCESS;
    }

	/**
	 * 换绑
	 * @param patientSNEntity
	 * @return
	 */
	@ApiOperation(value = "换绑",notes ="换绑")
	@RequestMapping(value = "/updateBinding", method = RequestMethod.POST)
	public ApiRes updateBinding(PatientSNEntity patientSNEntity, String oldSn){
		try {
			PatientSNEntity oldPatientSNEntity = new PatientSNEntity();
			oldPatientSNEntity.setSn(oldSn);
			oldPatientSNEntity.setIdCard(patientSNEntity.getIdCard());
			patientSNService.updateBinding(patientSNEntity, oldPatientSNEntity);
		} catch (ReturnException e) {
			return new ApiRes(false, null, null, e.getMessage());
		}
		return ApiRes.SUCCESS;
	}

    /**
     * 根据身份证和sn号取得测试数据
     * @param idCard
     * @param sn
     * @return
     */
	@ApiOperation(value = "根据身份证和sn号取得测试数据",notes ="根据身份证和sn号取得测试数据")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiRes queryByIdCardAndSn(PatientSNEntity entity, PageParam pageParam){
    	List<PatientSNEntity> entitys = patientSNService.search(entity, pageParam);
    	int count = patientSNService.count(entity);

		for (int i=0;i<entitys.size();i++){
			entitys.get(i).setCreateTime(null);
			entitys.get(i).setUpdateTime(null);
		}

		return new ApiRes(true, entitys, null, String.valueOf(count));
    }


}
